package com.avic.rong.activity.product;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.questionOrAnswer.activity.PersonalHomeActivity;
import com.avic.rong.utils.StatusBarUtil;

import org.json.JSONException;
import org.json.JSONObject;

import wendu.dsbridge.CompletionHandler;
import wendu.dsbridge.DWebView;

public class RankingListActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView iv_back;
    private TextView tvTitle;
    private ImageView iv_right;
    private LinearLayout ll_root;
    private DWebView webView;
    private ProgressBar progress_bar;

    private String articleUrl;
    private String mTitleStr = "排行榜";
    public static String ARG_URL = "arg_url";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ranking_list);

        StatusBarUtil.setStatusBarColor(RankingListActivity.this, Color.WHITE);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        initView();
        initData();

    }

    private void initData() {
        articleUrl = getIntent().getStringExtra(ARG_URL);
        tvTitle.setText(mTitleStr);

        webView.addJavascriptObject(this, null);
        if (Build.VERSION.SDK_INT >= 21) {
            webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        webView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                return true;
            }
        });
        webView.setWebChromeClient(new RankingListActivity.MyWebChromeClient());
        webView.loadUrl(articleUrl);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_back:
                if (webView.canGoBack()) {
                    webView.goBack();
                    return;
                }
                onBackPressed();
                break;
        }
    }

    private class MyWebChromeClient extends WebChromeClient {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            progress_bar.setProgress(newProgress);
            if (newProgress == 100) {
                progress_bar.setVisibility(View.GONE);
            }
            super.onProgressChanged(view, newProgress);
        }

        @SuppressLint("ResourceAsColor")
        @Override
        public void onReceivedTitle(WebView view, String title) {
            super.onReceivedTitle(view, title);
            if (!TextUtils.isEmpty(title)) {
                mTitleStr = title;
                tvTitle.setText(title);
                if ("排行榜".equals(mTitleStr)) {
                    tvTitle.setTextColor(Color.BLACK);
                    ll_root.setBackgroundResource(R.color.white);
                    iv_back.setColorFilter(Color.BLACK);
                }else{
                    tvTitle.setTextColor(Color.WHITE);
                    ll_root.setBackgroundResource(R.mipmap.top_bar_bg);
                    iv_back.setColorFilter(Color.WHITE);
                }
            } else {
                tvTitle.setText(mTitleStr);
            }
        }
    }

    private void initView() {
        iv_back = (ImageView) findViewById(R.id.iv_back);
        tvTitle = (TextView) findViewById(R.id.tv_title);
        iv_right = (ImageView) findViewById(R.id.iv_right);
        ll_root = (LinearLayout) findViewById(R.id.ll_root);
        webView = (DWebView) findViewById(R.id.webView);
        progress_bar = (ProgressBar) findViewById(R.id.progress_bar);
        iv_back.setOnClickListener(this);
    }

    @JavascriptInterface
    public void openUserCenter(Object msg, CompletionHandler<String> handler){
        JSONObject jsonObject = null;
        try {
            jsonObject = new JSONObject(msg.toString());
            String userId = jsonObject.getString("userId");
            Intent intent = new Intent(RankingListActivity.this, PersonalHomeActivity.class);
            intent.putExtra("homeUserID",userId);
            startActivity(intent);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    public static Intent queryIntent(Context context, String url) {
        Intent intent = new Intent(context, RankingListActivity.class);
        intent.putExtra(ARG_URL, url);
        return intent;
    }
}
